function Get-FTPFile ($Source,$Target,$UserName,$Password) 
 { 
     $ftprequest = [System.Net.FtpWebRequest]::create($Source) 
     $ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password) 
     $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
     $ftprequest.UseBinary = $true 
     $ftprequest.KeepAlive = $false 
      
     $ftpresponse = $ftprequest.GetResponse() 
     $responsestream = $ftpresponse.GetResponseStream() 
      
    $targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create) 
     [byte[]]$readbuffer = New-Object byte[] 1024 
      
     do{ 
         $readlength = $responsestream.Read($readbuffer,0,1024) 
         $targetfile.Write($readbuffer,0,$readlength) 
     } 
     while ($readlength -ne 0) 
      
     $targetfile.close() 
}
function Unzip($zipPath, $destination){
    $shell = new-object -com shell.application;
    $zip = $shell.NameSpace($zipPath);
    foreach($item in $zip.items()){
        $shell.Namespace($destination).copyhere($item)
	$it = $destination + "\" + $item.Name
	Rename-Item $it "import.csv"
    }
}      
     

$d = Get-Date
$dir = "C:\inetpub\wwwroot\imports\" + $d.Month + "-" + $d.Day + "-" + $d.Year
[IO.Directory]::CreateDirectory($dir) 

$file = $dir + "\import.zip"

Get-FTPFile "ftp://utility.dbo2.com/DataNetExtract_Main_10280.csv.zip" $file "DataNet_10280" "S@fety10280"

Remove-Item "C:\inetpub\wwwroot\imports\import.csv" -ErrorAction SilentlyContinue

Unzip $file "C:\inetpub\wwwroot\imports"


